﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema
{
    public partial class QuanLyLoaiPhongChieu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsLPC();
        }

        public void LaydsLPC()
        {
            DataTable ds_lpc = Timkiem_MA("");
            
            for (int i = 0; i < ds_lpc.Rows.Count; i++)
            {
                string str = ds_lpc.Rows[i]["MaLP"].ToString();
                DDLMaLPC.Items.Add(str);
            }
            grLoaiPC.DataSource = ds_lpc;
            grLoaiPC.DataBind();
        }


        protected void DDLMaLPC_SelectedIndexChanged(object sender, EventArgs e)
        {
           // string s = "select * from LoaiPhongchieu where malp='" + DDLMaLPC.SelectedItem.ToString() + "'";
            DataTable dt = Timkiem_MA(DDLMaLPC.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtTenLPC.Text = dt.Rows[0][1].ToString();
              
            }
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@tenlp", txtTenLPC.Text.ToString()));
                CDatastore.ThucHienStore("SP_INSERT_LOAIPHONGCHIEU", sqlPara);
                lblThongbao.Text = "Bạn đã thêm Loại Phòng Chiếu thành công";
                DDLMaLPC.Items.Clear();
                txtTenLPC.Text = null;
                LaydsLPC();
            }
            catch (Exception ex)
            {
                lblThongbao.Text = ex.Message;
            }
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@malp", DDLMaLPC.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_LOAIPHONGCHIEU", sqlPara);
                lblThongbao.Text = "Bạn đã xóa Mã Loại Phòng Chiếu " + DDLMaLPC.Text + " thành công";
                DDLMaLPC.Items.Clear();
                LaydsLPC();
                txtTenLPC.Text = null;


            }
            catch (Exception ex)
            {
                lblThongbao.Text = ex.Message;
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@malp", DDLMaLPC.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tenlp", txtTenLPC.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_LOAIPHONGCHIEU", sqlPara);
                lblThongbao.Text = "Bạn đã cập nhật Mã Loại Phòng Chiếu " + DDLMaLPC.Text + " thành công";
                DDLMaLPC.Items.Clear();
                LaydsLPC();
                txtTenLPC.Text = null;
            }
            catch (Exception ex)
            {
                lblThongbao.Text = ex.Message;
            }
        }

        protected void grLoaiPC_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLMaLPC.Text = grLoaiPC.SelectedRow.Cells[1].Text;
            txtTenLPC.Text = grLoaiPC.SelectedRow.Cells[2].Text;
        }


        public DataTable Timkiem_Ten(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@tenlp", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_LOAIPHONGCHIEU_TENLP", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongbao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_LOAIPHONGCHIEU_TENLP");
        }
        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAlp", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_LOAIPHONGCHIEU_MALP", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongbao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_LOAIPHONGCHIEU_MALP");
        }

        protected void bttimkiem_Click(object sender, EventArgs e)
        {
            DataTable dt = Timkiem_Ten(txtTenLPC.Text);
            grLoaiPC.DataSource = dt;
            grLoaiPC.DataBind();
        }

        protected void grLoaiPC_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grLoaiPC.PageIndex = e.NewPageIndex;
            grLoaiPC.DataBind();
            LaydsLPC();
        }
    }
}